package com.houzehui.demo.controller;

import cn.hutool.core.io.FileUtil;
import cn.hutool.extra.qrcode.QrCodeUtil;
import com.houzehui.demo.config.MsgResult;
import com.houzehui.demo.config.QrCodeUtils;
import com.houzehui.demo.config.WebUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/qrcode")
public class qrcodeController {

    @Autowired
    RedisTemplate redisTemplate;

    @Value("${file.domain}")
    String domain;

    @Value("${file.path}")
    String path;


    /**
     * 根据商品信息生成二维码 返回二维码的返回地址
     * @param name
     * @param price
     * @param id
     * @return
     */
    @GetMapping("getqrcode")
    public MsgResult getqrcode(String name,String price,String id){
        String token = WebUtils.getRequest().getHeader("token");
        Boolean lock = redisTemplate.opsForValue().setIfAbsent(token, null, 10, TimeUnit.SECONDS);
        if(!lock){
            return null;
        }

        String fileName=token+".jpg";

//        QrCodeUtils.zxingCodeCreate("www.baidu.com","d:/pic/"+id+".jpg",500,"d:/pic/wang1.png");
        QrCodeUtil.generate("赵旭程 你在干啥呢",300,300, FileUtil.file(path+fileName));
        return MsgResult.succeed(domain+fileName);

    }
}
